package com.zdsoft.blp.zongbuju.shouye.linliquan.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.LinLi;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/12.
 */
public class LLHFActivity extends BaseActivity implements NetCallback {
    private TextView tv_send;
    private EditText et_content;
    private FanHuiView title;
    private LinLi linLi;
    private Intent intent;
    private String uerrId,id,msg,content;
    private int num;//用于辨别 解析的类型
    @Override
    protected void init() {
        setContentView(R.layout.linli_pinglun_fb);
        tv_send=find(R.id.tv_send);
        et_content=find(R.id.et_content);
        title=find(R.id.title);
        title.getTv_title().setText("回复");
        //Edittext 获取焦点
        et_content.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                et_content.requestFocus();
            }
        });
        //获取用户id
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        uerrId = sp.getString("userId","");
        linLi=new LinLi();
      //  接收信息
        intent=getIntent();
        id=intent.getStringExtra("id");
        tv_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("1".equals(id)){
                content=(et_content.getText().toString().trim());
                    if (!"".equals(content)){
                        LTgetNet(content);//邻里论坛回复内容
                    }else {
                        Toast.makeText(LLHFActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
                    }
                }else if ("2".equals(id)){
                    content=(et_content.getText().toString().trim());
                    if (!"".equals(content)){
                        PCgetNet(content);
                    }else {
                        Toast.makeText(LLHFActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
                    }
                }else if ("3".equals(id)){
                    content=(et_content.getText().toString().trim());
                    if (!"".equals(content)){
                        ESgetNet(content);
                    }else {
                        Toast.makeText(LLHFActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
                    }
                }else if ("4".equals(id)){
                    content=(et_content.getText().toString().trim());
                    if (!"".equals(content)){
                        LYgetNet(content);
                    }else {
                        Toast.makeText(LLHFActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
                    }
                }else if ("5".equals(id)){
                    content=(et_content.getText().toString().trim());
                    if (!"".equals(content)){
                        QZgetNet(content);
                       // XQQZZPActivity activity=new XQQZZPActivity();activity.getNet();
                    }else {
                        Toast.makeText(LLHFActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
                    }
                }else if ("6".equals(id)){
                    content=(et_content.getText().toString().trim());
                    if (!"".equals(content)){
                       XWgetNet(content);
                    }else {
                        Toast.makeText(LLHFActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
                    }
                }else if ("7".equals(id)){
                    content=(et_content.getText().toString().trim());
                    if (!"".equals(content)){
                        ZSgetNet(content);
                    }else {
                        Toast.makeText(LLHFActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });


    }

    private void LYgetNet(String content) {
        Net net=new Net();
        net.url= API.LYINFO_URL;
        net.nameSpace=API.LYINFO_NAMESPACE;
        net.methodName=API.LYREPLYSAVE;//lyReplySave
        Map map=new HashMap();
        map.put("replyUser",uerrId);
        map.put("content",content);
        map.put("infoId",linLi.getId());
        map.put("id","");
        map.put("parentId","");
        net.parms=map;
        getServer(net,this);
    }

    private void PCgetNet(String content) {
        Net net=new Net();
        net.url= API.CARPOOLINGINFO_URL;
        net.nameSpace=API.CARPOOLINGINFO_NAMESPACE;
        net.methodName=API.CARPOOLINGREPLYSAVE;//carPoolingReplySave
        Map map=new HashMap();
        map.put("replyUser",uerrId);
        map.put("content",content);
        map.put("infoId",linLi.getId());
        map.put("id","");
        map.put("parentId","");
        net.parms=map;
        getServer(net,this);
    }

    private void ZSgetNet(String content) {
        Net net = new Net();
        net.url = API.INFOEXCHANGE_URL;//infoExchangeService
        net.nameSpace = API.INFOEXCHANGE_NAMESPACE;
        net.methodName=API.HOUSERENTALREPLSAVE;//houseRentalReplySave
        Map map=new HashMap();
        map.put("userId",uerrId);
        map.put("content",content);
        map.put("infoId",linLi.getId());
        map.put("id","");
        net.parms=map;
        getServer(net,this);
    }

    //寻物招领
    private void XWgetNet(String content) {
        Net net = new Net();
        net.url = API.INFOEXCHANGE_URL;//infoExchangeService
        net.nameSpace = API.INFOEXCHANGE_NAMESPACE;
        net.methodName=API.FOUNTLOSTREPLYSAVE;//foundLostReplySave
        Map map=new HashMap();
        map.put("userId",uerrId);
        map.put("content",content);
        map.put("infoId",linLi.getId());
        map.put("id","");
        net.parms=map;
        getServer(net,this);
    }

    //求职招聘
    private void QZgetNet(String content) {
        Net net=new Net();
        net.url= API.INFOEXCHANGE_URL;
        net.nameSpace=API.INFOEXCHANGE_NAMESPACE;
        net.methodName=API.SEARCHJOBREPLYSAVE;//searchJobReplySave
        Map map=new HashMap();
        map.put("userId",uerrId);
        map.put("content",content);
        map.put("infoId",linLi.getId());
        map.put("id","");
        net.parms=map;
        getServer(net,this);
    }

    //二手市场
    private void ESgetNet(String content) {
        Net net=new Net();
        net.url= API.ESINFO_URL;
        net.nameSpace=API.ESINFO_NAMESPACE;
        net.methodName=API.ESREPLYSAVE;//esReplySave
        Map map=new HashMap();
        map.put("replyUser",uerrId);
        map.put("content",content);
        map.put("secondInfoId",linLi.getId());
        map.put("id","");
        net.parms=map;
        getServer(net,this);
    }

    //邻里论坛
    private void LTgetNet(String content) {
        Net net=new Net();
        net.nameSpace= API.BBS_NAMESPACE;
        net.url=API.BBS_URL;
        net.methodName=API.BBSREPLYSAVE;//bbsReplySave
        Map<String,Object> map=new HashMap<>();
        map.put("userId",uerrId);//发布或者更新回复用户的id
        map.put("content",content);
        map.put("bbsTopicId",linLi.getId());//主题帖子的id
        net.parms=map;
        getServer(net,this);
    }


    @Override
    public void onResponse(boolean flag, int code, String res) {
            Log.e("PINGLUN",res);
            try {
                JSONObject object=new JSONObject(res);
                msg=object.getString("msg");
                Toast.makeText(LLHFActivity.this,msg,Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }

    }


}
